/**
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-03 14:14:47
 * Copyright (c) 2015- joysuch-版权所有
 */
package com.joysuch.wwyt.bp.controller;

import com.joysuch.wwyt.bp.bean.BpManufacturingTechAddBean;
import com.joysuch.wwyt.bp.bean.BpManufacturingTechListBean;
import com.joysuch.wwyt.bp.bean.BpMonitoringPointBean;
import com.joysuch.wwyt.bp.entity.BpManufacturingTech;
import com.joysuch.wwyt.bp.repository.BpManufacturingTechDao;
import com.joysuch.wwyt.bp.service.BpManufacturingTechService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(tags = "设备设施管理-工艺流程")
@RequestMapping("/bp/manufacturing")
public class BpManufacturingTechController {

    private static final Logger logger = LoggerFactory.getLogger(BpManufacturingTechController.class);

    @Autowired
    private BpManufacturingTechService bpManufacturingTechService;

    @Autowired
    private BpManufacturingTechDao bpManufacturingTechDao;

    @PostMapping("add")
    public Object add(@RequestBody @Valid BpManufacturingTechAddBean entity) {
        String errorMessage = bpManufacturingTechService.add(entity);
        if (StringUtils.isNotBlank(errorMessage)) {
            throw new IllegalArgumentException(errorMessage);
        }
        return ResultBean.success("");
    }

    @PostMapping("update")
    public ResultBean update(@RequestBody @Valid BpManufacturingTechAddBean entity) {
        if (entity.getId() == null) {
            return ResultBean.fail(102, "未指定主键");
        }
        String errorMessage = bpManufacturingTechService.update(entity);
        if (StringUtils.isNotBlank(errorMessage)) {
            throw new IllegalArgumentException(errorMessage);
        }
        return ResultBean.success("");
    }

    @RequestMapping("page")
    public ResultBean page(Pageable page) {
        try {
            page = PageDataUtils.addOrderByDesc(page, "id");
            List<BpManufacturingTechListBean> data = bpManufacturingTechService.findByPage(page);
            List<BpManufacturingTech> all = bpManufacturingTechDao.findAll();
            return ResultBean.pageData(data, all.size());
        } catch (Exception e) {
            logger.error("分页查询失败", e);
            return ResultBean.fail(101, "分页查询失败");
        }
    }

    @RequestMapping("delete")
    public Object delete(Long id) {
        try {
            bpManufacturingTechService.delete(id);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("删除失败", e);
            return ResultBean.fail(101, "删除失败");
        }
    }


    @GetMapping("findAll")
    public ResultBean findAll(Pageable page) {
        List<BpMonitoringPointBean> all = bpManufacturingTechService.findAll(page);
        return ResultBean.wrapSuccessfulResult(all);
    }


    @GetMapping("getById")
    public ResultBean getById(Long id) {
        BpManufacturingTechAddBean byId = bpManufacturingTechService.getById(id);
        return ResultBean.wrapSuccessfulResult(byId);
    }


}
